class Solution
{
public:
    // 没啥技术含量的模拟
    vector<int> survivedRobotsHealths(vector<int> &positions, vector<int> &healths, string directions)
    {
        struct Robot
        {
            int id;
            int position;
            int health;
            char direction;
        };

        int n = positions.size();
        vector<Robot> robots;
        for (int i = 0; i < n; ++i)
        {
            robots.push_back({i + 1, positions[i], healths[i], directions[i]});
        }

        sort(robots.begin(), robots.end(), [](auto &r1, auto &r2)
             { return r1.position < r2.position; });

        list<Robot> robotList(robots.begin(), robots.end());

        auto p1 = robotList.begin();
        while (true)
        {
            if (p1 == robotList.end())
            {
                break;
            }
            auto p2 = p1;
            ++p2;

            if (p2 == robotList.end())
            {
                break;
            }

            if (p1->direction == 'R' && p2->direction == 'L')
            {
                if (p1->health == p2->health)
                {
                    auto nextP = p1;
                    if (p1 != robotList.begin())
                    {
                        --nextP;
                    }
                    else
                    {
                        nextP = p2;
                        ++nextP;
                    }
                    robotList.erase(p1);
                    robotList.erase(p2);
                    p1 = nextP;
                }
                else if (p1->health > p2->health)
                {
                    robotList.erase(p2);
                    p1->health -= 1;
                    if (p1->health == 0)
                    {
                        auto nextP = p1;
                        if (p1 != robotList.begin())
                        {
                            --nextP;
                        }
                        else
                        {
                            ++nextP;
                        }
                        robotList.erase(p1);
                        p1 = nextP;
                    }
                }
                else
                {
                    robotList.erase(p1);
                    p2->health -= 1;
                    if (p2->health == 0)
                    {
                        auto nextP = p2;
                        if (p2 != robotList.begin())
                        {
                            --nextP;
                        }
                        else
                        {
                            ++nextP;
                        }
                        robotList.erase(p2);
                        p1 = nextP;
                    }
                    else
                    {
                        if (p2 != robotList.begin())
                        {
                            --p2;
                        }
                        p1 = p2;
                    }
                }
            }
            else
            {
                ++p1;
            }
        }

        robots = {robotList.begin(), robotList.end()};
        sort(robots.begin(), robots.end(), [](auto &r1, auto &r2)
             { return r1.id < r2.id; });

        vector<int> result;
        for (auto &robot : robots)
        {
            result.push_back(robot.health);
        }

        return result;
    }
};